class UserCollectionsController < ApplicationController
  # GET /user_collections
  # GET /user_collections.json
  def index
  #  @user_collections = UserCollection.all
    # get the user collections of the current logged in user 
    
    if current_user
      @user_collections = UserCollection.joins(:user, :collection).where("user_id = ?", current_user.id)
        
      respond_to do |format|
        format.html # index.html.erb
        format.json { render :json => @user_collections }
      end
      
    else
      redirect_to_login
    end
      
      
  end

  # GET /user_collections/1
  # GET /user_collections/1.json
  def show
    
    if current_user
    
      @user_collection = UserCollection.find(params[:id])

      respond_to do |format|
        #format.html # show.html.erb
        format.html { redirect_to :action => 'index', :controller => 'user_books', :collection_id => @user_collection.collection_id }
        #render :json => @user_collection
      end
      
    else
      flash.now.alert = "Login first please"
      render "sessions/new"
    end
      
  end
  
  # GET /user_collections/new
  # GET /user_collections/new.json
  def new
    flash.keep
    @user_collection = UserCollection.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @user_collection }
    end
  end

  # GET /user_collections/1/edit
  def edit
    @user_collection = UserCollection.find(params[:id])
    
      respond_to do |format|
        format.html { redirect_to :action => 'edit', :controller => 'user_books', :collection_id => @user_collection.collection_id }
      end
        
  end

  # POST /user_collections
  # POST /user_collections.json
  def create
    @user_collection = UserCollection.new(params[:collection])
    @user_collection.user_id=current_user.id
    if UserCollection.find_all_by_collection_id_and_user_id(params[:collection][:collection_id].to_s, current_user.id).empty?
    
    respond_to do |format|
      if @user_collection.save
        format.html { redirect_to @user_collection, :notice => 'User collection was successfully created.' }
        format.json { render :json => @user_collection, :status => :created, :location => @user_collection }
      else
        format.html { render :action => "new" }
        format.json { render :json => @user_collection.errors, :status => :unprocessable_entity }
      end
    end
    
    else
      respond_to do |format|
        format.html { render :action => "new", flash[:notice] => "Your already have this collection !" }
        format.json { render :json => @user_collection.errors, :status => :unprocessable_entity }
      end
    end
    
    
  end

  # PUT /user_collections/1
  # PUT /user_collections/1.json
  def update
    @user_collection = UserCollection.find(params[:id])

    respond_to do |format|
      if @user_collection.update_attributes(params[:user_collection])
        format.html { redirect_to @user_collection, :notice => 'User collection was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @user_collection.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /user_collections/1
  # DELETE /user_collections/1.json
  def destroy
    @user_collection = UserCollection.find(params[:id])
    @user_collection.destroy

    respond_to do |format|
      format.html { redirect_to user_collections_url }
      format.json { head :no_content }
    end
  end
end
